package com.zfz.happymall.web;

import com.zfz.happymall.entity.Account;
import com.zfz.happymall.entity.DataVo;
import com.zfz.happymall.service.AccountService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("account")
@RestController
public class AccountController {

    @Resource(name = "AccountService")
    private AccountService service;

    @GetMapping("all")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>();
        List<Account> accountList = service.selectAll();
        if (accountList != null) {
            map.put("code", "0000");
            map.put("msg", "success");
            map.put("data", accountList);
        } else {
            map.put("code", "0001");
            map.put("msg", "fail");
        }
        return map;
    }

    @DeleteMapping("deleteAccount/{id}")
    public Map<String, Object> deleteAccount(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        int count = service.deleteById(id);
        if (count == 0) {
            map.put("code", "0001");
            map.put("msg", "删除失败");
        } else {
            map.put("code", "0000");
            map.put("msg", "删除成功");
        }
        return map;
    }

    //新增
    @PostMapping("insertAccount")
    public Map<String, Object> saveAccount(@RequestBody Account account) {
        Map<String, Object> map = new HashMap<>();
        int count = service.save(account);
        if (count == 0) {
            map.put("code", "0001");
            map.put("msg", "添加失败");
        } else {
            map.put("code", "0000");
            map.put("msg", "添加成功");
        }
        return map;
    }

    //修改
    @PostMapping("updateAccount")
    public Map<String, Object> updateAccount(@RequestBody Account account) {
        Map<String, Object> map = new HashMap<>();
        int count = service.update(account);
        if (count == 0) {
            map.put("code", "0001");
            map.put("msg", "修改失败");
        } else {
            map.put("code", "0000");
            map.put("msg", "修改成功");
        }
        return map;
    }

    //查询图表信息
    @GetMapping("getLineData")
    public Map<String, Object> getLineDataAccount() {
        Map<String, Object> map = new HashMap<>();
        List<DataVo> list1 = service.getLineData(1);//收入
        List<DataVo> list2 = service.getLineData(2);//支出
        map.put("list1", list1);
        map.put("list2", list2);
        return map;
    }
}
